package com.cskaoyan.gateway.controller.user;

import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.user.annotation.Anoymous;
import com.mall.user.intercepter.TokenIntercepter;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @program: project4-mall
 * @time: 2021/11/2
 * @author: yangshuai
 **/

@RestController
@RequestMapping("/user")
public class LoginOutController {

    /**
     * 退出登录（清除cookie中的jwt）
     * @return
     */
    @GetMapping("/loginOut")
    public ResponseData loginOut(HttpServletRequest request, HttpServletResponse response){
        // 获取cookie
        Cookie[] cookies = request.getCookies();
        // cookies不等于0
        if (cookies != null){
            // 遍历cookies
            for (Cookie cookie : cookies) {
                // 清除cookie
                if (TokenIntercepter.ACCESS_TOKEN.equals(cookie.getName())){
                    // 设置过期时间
                    cookie.setMaxAge(0);    // 不记录cookie
                    // 设置路径
                    cookie.setPath("/");    // 同一服务器内共享cookie
                    // 将cookie写入浏览器
                    response.addCookie(cookie);
                }
            }
        }
        return new ResponseUtil().setData(null);
    }
}
